package com.bafa.front.events;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tools.ant.taskdefs.SendEmail;

import com.bafa.dbcache.LeagueCache;
import com.bafa.email.Email;
import com.bafa.front.utils.Ajax;
import com.bafa.objects.PageBean;
import com.bafa.services.RetrieveServices;

public class SendOrderEvent extends Event {

	@Override
	public void doEvent(HttpServletRequest request,
			HttpServletResponse response, PageBean page) {

		String name = request.getParameter("firstname");
		String surname = request.getParameter("surname");
		String pkg = request.getParameter("package");
		String emailAddress = request.getParameter("email");
		String subdomain = request.getParameter("subdomain");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String extra = request.getParameter("extras");
		boolean throwError = false;
		if (!new Ajax().checkDomain(subdomain).equals("")) {
			message
					.addError("The subdomain has already been chosen. Please choose another.");
			throwError = true;
		}
		if(emailAddress.equals("")||emailAddress.indexOf("@")<0){
			message
					.addError("Your email address is not valid. Please enter a valid email address");
			throwError=true;
		}
		if(throwError){
			page.addDetails(RetrieveServices.getPage("order"));
		
		}else {
			StringBuffer email = new StringBuffer();
			email.append("Dear " + name);
			email
					.append("\n\n Thank you for choosing myfootyleague.com as your online football league administration software.");
			email
					.append("\n\n You will receive an email within the next 24 hours to confirm the setup of your website and the beginning of your free 10 day trial.");
			email
					.append("\n The email will also request payment through the click of a button via the paypal site. This is a quick and easy method of payment and ensures total security. As soon as your payment is received your 1 year subscription begins and you can start enjoying the use of MyFootyLeague.com");

			email.append("\n\n Confirmation of requested package:");
			email.append("\n\n - " + pkg + " package");
			email.append("\n - Extra comments: " + extra);

			email
					.append("\n\n If you have any queries please do not hesitate to email us at support@myfootyleague.com");
			String[] emails = { emailAddress };
			Email.sendEmail("myfootyleague@myfootyleague.com", emails,
					"Confirmation of Registration", email.toString());
			String[] myEmail = { "r.hmeid@gmail.com" };
			email.append("\n\n Please setup the package for " + emailAddress);
			Email.sendEmail("myfootyleague@myfootyleague.com", myEmail,
					"Confirmation of Registration", email.toString());
		}
	}

}
